package org.aceor.mddal.group;

import com.google.common.collect.Maps;
import org.aceor.mddal.group.util.RuntimeSelectorHolder;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by lxue on 16/3/27.
 */
public class GroupDataSource implements DataSource{
    private String dbGroupKey;

    private Map<String/* AtomId */, DataSourceWrapper/* Wrapper过的Atom DS */> dataSourceWrapperMap = Maps.newHashMap();

    private RuntimeSelectorHolder runtimeSelectorHolder = new RuntimeSelectorHolder();

    public GroupDataSource(String dbGroupKey) {
        this.dbGroupKey = dbGroupKey;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return new GroupConnection(this);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return new GroupConnection(username, password, this);
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    public String getDbGroupKey() {
        return dbGroupKey;
    }

    public void setDbGroupKey(String dbGroupKey) {
        this.dbGroupKey = dbGroupKey;
    }

    public Map<String, DataSourceWrapper> getDataSourceWrapperMap() {
        return dataSourceWrapperMap;
    }

    public void setDataSourceWrapperMap(Map<String, DataSourceWrapper> dataSourceWrapperMap) {
        this.dataSourceWrapperMap = dataSourceWrapperMap;
    }

    public RuntimeSelectorHolder getRuntimeSelectorHolder() {
        return runtimeSelectorHolder;
    }

    public void init() {
        runtimeSelectorHolder.init(dataSourceWrapperMap);
    }
}
